package com.goodomen.garbage.web.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;

/**
 * @author : Zhao Da
 * @since : 2019/5/5 17:09
 */
@Slf4j
public class AESUtil {

    public static String getEncrypt(String sSrc, String skey) {
        if (StringUtils.isNotBlank(sSrc)) {
            try {
                sSrc = encodeBase64(sSrc);
                return encrypt(sSrc, skey);
            } catch (Exception var3) {
                log.error("aes encrypt error, source: {}, msg: {}", new Object[]{sSrc, var3.getMessage()});
            }
        }

        return null;
    }

    public static String getDecrypt(String sSrc, String skey) {
        if (StringUtils.isNotBlank(sSrc)) {
            try {
                sSrc = decrypt(sSrc, skey);
                return decodeBase64(sSrc);
            } catch (Exception var3) {
                log.error("aes decrypt error, source: {}, msg: {}", new Object[]{sSrc, var3.getMessage()});
            }
        }

        return null;
    }

    private static String encodeBase64(String str) throws UnsupportedEncodingException {
        return new String(Base64.encodeBase64(str.getBytes("utf8")));
    }

    private static String decodeBase64(String str) throws UnsupportedEncodingException {
        return new String(Base64.decodeBase64(str), "utf8");
    }

    private static String encrypt(String msg, String skey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(skey.getBytes());
        kgen.init(128, random);
        SecretKey key = kgen.generateKey();
        Cipher ecipher = Cipher.getInstance("AES");
        ecipher.init(1, key);
        return asHex(ecipher.doFinal(msg.getBytes()));
    }

    private static String decrypt(String value, String skey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(skey.getBytes());
        kgen.init(128, random);
        SecretKey key = kgen.generateKey();
        Cipher dcipher = Cipher.getInstance("AES");
        dcipher.init(2, key);
        return new String(dcipher.doFinal(asBin(value)));
    }

    private static String asHex(byte[] buf) {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);

        for(int i = 0; i < buf.length; ++i) {
            if ((buf[i] & 255) < 16) {
                strbuf.append("0");
            }

            strbuf.append(Long.toString((long)(buf[i] & 255), 16));
        }

        return strbuf.toString();
    }

    private static byte[] asBin(String src) {
        if (src.length() < 1) {
            return null;
        } else {
            byte[] encrypted = new byte[src.length() / 2];

            for(int i = 0; i < src.length() / 2; ++i) {
                int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
                encrypted[i] = (byte)(high * 16 + low);
            }

            return encrypted;
        }
    }
}
